import { Color, Graphics, UITransform } from 'cc';
import { ccclass, cccomponent, ccmodifier } from '../decorators';
import { BaseStack } from './BaseStack';

/**
 * - 作者: DoooReyn
 * - 日期: 2023.10.31
 * - 名称: Popup
 * - 描述: 视图-弹窗层
 */
@ccclass()
@ccmodifier('Popup')
export class Popup extends BaseStack {
    /** 绘制组件 */
    @cccomponent(Graphics, '.')
    private __graphics: Graphics;

    /** 黑色半透背景是否已绘制 */
    private __draw: boolean = false;

    protected onCheckInteractable(): void {
        super.onCheckInteractable();

        if (this._interactable) {
            if (this.__draw) return;

            this.__draw = true;
            const weight = this.__graphics.lineWidth;
            const { width, height } = this.node.getComponent(UITransform)!;
            const halfWidth = width / 2 || 0;
            const halfHeight = height / 2 || 0;
            this.__graphics.fillColor = new Color('#000000a0');
            this.__graphics.fillRect(-halfWidth - weight, -halfHeight - weight, width + weight, height + weight);
            this.__graphics.enabled = true;
        } else {
            this.__graphics.clear();
            this.__draw = false;
            this.__graphics.enabled = false;
        }
    }

    async exit(): Promise<void> {
        this.__graphics.clear();
        this.__draw = false;
        this.__graphics.enabled = false;
        super.exit();    
    }
}
